﻿namespace HelloWorld.Startup.Localization
{
    //.NET References
    using System;
    using System.Net;
    using System.ServiceModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    //HelloWorld References
    using HelloWorld.Startup.LocalizationService;
    using System.Threading;

    /// <summary>
    /// Represents a localization service. It provides resouces' localization.
    /// </summary>
    public class LocalizationService
    {
        //Fields

        private static Object _syncObj = new Object();

        //Methods

        /// <summary>
        /// Translate the resource with resourceID equals to the <paramref name="resourceID"/> parameter.
        /// </summary>
        /// <param name="resourceID">Resource identifier.</param>
        /// <param name="langCode">Language code.</param>
        /// <returns></returns>
        public static Object Translate(String resourceID, String langCode) 
        {
            Object result = null;
            lock (LocalizationService._syncObj)
            {
                bool completed = false;
                LocalizationServiceClient client = new LocalizationServiceClient();
                client.GetResourceValueCompleted += new EventHandler<GetResourceValueCompletedEventArgs>(
                    (_sender, _e) =>
                    {
                        result = _e.Result;
                        completed = true;
                    });
                client.GetResourceValueAsync(resourceID, langCode);
                while (!completed)
                {
                    Thread.Sleep(100);
                }   
            }
            return result;
        }
    }
}
